package kit.views;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public abstract class KitPanel extends JPanel {
	
	protected File tempFileBuilder(String originalFileName) {
		String fileName = String.format("%s_%s.kit", originalFileName,
				originalFileName.hashCode());
		File file = new File(fileName);
		return file;
	}

	protected File showSaveDialog(String title, String errorMessage,
			JFrame frame) {
		File savedFile = displayFileSaverDialog(title, frame);
		if (savedFile == null) {
			showErrorDialog(errorMessage, frame);
		}
		return savedFile;
	}

	protected void showErrorDialog(String errorMessage, JFrame frame) {
		JOptionPane.showMessageDialog(frame,
				errorMessage, "Errore",
				JOptionPane.ERROR_MESSAGE);
	}
	
	protected File showLoadDialog(String title, String errorMessage,
			JFrame frame) {
		File loadedFile = displayFileLoaderDialog(title, frame);
		if (loadedFile == null) {
			showErrorDialog(errorMessage, frame);
		}
		return loadedFile;
	}

	private File displayFileSaverDialog(String title, JFrame frame) {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setDialogTitle(title);
		if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			return file;
		}
		return null;
	}
	
	private File displayFileLoaderDialog(String title, JFrame frame) {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setDialogTitle(title);
		if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			return file;
		}
		return null;
	}
}
